package com.example.testdemo.eventbus.queue

/**
 * Create Time:2020/8/13
 * Author: Lindanrong
 * Description:
 */
class BeanPost constructor(var bean: Bean?) {

    var next: BeanPost? = null

    companion object {
        private var beanPostPool: ArrayList<BeanPost> = ArrayList<BeanPost>()

        fun obtainBeanPost(bean: Bean): BeanPost {
            synchronized(beanPostPool) {
                var size = beanPostPool.size
                if (size > 0) {
                    var beanPost = beanPostPool.removeAt(size - 1)
                    beanPost.bean = bean
                    beanPost.next = null
                    return beanPost
                }
            }
            return BeanPost(bean)
        }

        fun releaseBeanPost(beanPost: BeanPost) {
            beanPost.bean = null
            beanPost.next = null
            synchronized(beanPostPool) {
                if (beanPostPool.size < 1000) {
                    beanPostPool.add(beanPost)
                }
            }
        }

    }


}

data class Bean(var name: String)